import { fetchChartData, fetchCardData, fetchRequestAll, fetchRequest2 } from '../services/HomePage';
import { message } from 'antd';
import { parse } from 'qs';

export default {
    namespace: 'HomePage',
    state: {
        list1: [],
        list2: [],
        startDate: "",
        endDate: "",
        hiddenNil: true,
        currency: "99",
        uniqueName: "",
        nextUName: "",
        indexWhere: "",
        indexID: "",
        datas: [],
        cardDatas: [],
        chartDatas: [],
        homeIndexShow: "none",//首页指标显示
    },
    effects: {
        *fetchAll({ payload: { params } }, { call, put }) {
            const datas = yield call(fetchRequestAll, params);
            if (datas) {
                yield put({
                    type: 'fetchAllSuccess',
                    payload: {
                        datas,
                        params
                    },
                });
            }
        },
        *fetchChartData({ payload: { params } }, { call, put }) {
            const cardDatas = yield call(fetchCardData, params);
            const chartDatas = yield call(fetchChartData, params);
            if (cardDatas && chartDatas) {
                yield put({
                    type: 'setChartDatas',
                    payload: {
                        cardDatas,
                        chartDatas,
                        params
                    },
                });
            }
        },
        *fetch2({ payload: { params } }, { call, put }) {
            const data2 = yield call(fetchRequest2, params);
            if (data2) {
                yield put({
                    type: 'fetch2Success',
                    payload: {
                        data2,
                        params
                    },
                });
            }
        }
    },
    reducers: {
        goDwon(state, { payload: { params, hasColHeader, dataMaps, columns, step, isNewMap, dataMapsChange } }) {
            return { ...state, params, hasColHeader, dataMaps, columns, step, isNewMap, dataMapsChange };
        },
        goUp(state, { payload: { step, success, successCount, errorDatas } }) {
            const dataMapsChange = false
            return { ...state, step, success, successCount, errorDatas, dataMapsChange };
        },
        fetchAllSuccess(state, { payload: { datas, params } }) {
            return { ...state, ...params, ...datas, homeIndexShow: "" };
        },
        setChartDatas(state, { payload: { cardDatas, chartDatas, params } }) {
            return { ...state, ...params, cardDatas, chartDatas };
        },
        fetch2Success(state, { payload: { data2, params } }) {
            return { ...state, ...params, ...data2 };
        },
        setHomeIndexShow(state, { payload: { homeIndexShow } }) {
            return { ...state, homeIndexShow };
        }
    }
};
